<?php

class Core_Frontend_Module_Poi_Latest extends Dcmp_Module_Base {

    protected $name = 'poi_latest';

    public function install() {

    }

    public function uninstall() {

    }

    public function buildConfig($form) {
        $form->addBooleanField($this->__('share.label.is_show_index'), 'is_show_index', 'module[parameters][is_show_index]')
                ->setYesOption($this->__('status.boolean.1'))
                ->setNoOption($this->__('status.boolean.0'));
        $form->addTextBoxField($this->__('share.label.headline_index_count'), 'headline_index_count', 'module[parameters][headline_index_count]')
                ->addDigitsRule();
        $form->addSelectField($this->__('share.label.sort_condition'),'sort_condition','module[parameters][sort_condition]')
                ->setOption($this->__('share.label.clickcount'), 'click_count')
                ->setOption($this->__('share.label.modified_time'), 'modified_time');
        $form->addTextBoxField($this->__('share.label.item_count'),'item_count','module[parameters][item_count]')
             ->addRequiredRule()
             ->addDigitsRule();
        $form->addTextBoxField($this->__('share.label.image_count'),'image_count','module[parameters][image_count]')
             ->setValue(0)
             ->addDigitsRule();
        $form->addTextBoxField($this->__('share.label.introduction_count'),'introduction_count','module[parameters][introduction_count]')
             ->addDigitsRule();
        $form->addTextBoxField($this->__('share.label.introduction_size'),'introduction_size','module[parameters][introduction_size]')
             ->addDigitsRule();
        $form->addTextBoxField($this->__('share.label.image_width'),'image_width','module[parameters][image_width]')
             ->setValue(0)
             ->addDigitsRule();
        $form->addTextBoxField($this->__('share.label.image_height'),'image_height','module[parameters][image_height]')
             ->setValue(0)
             ->addDigitsRule();
        $form->createTab('module_option', $this->__('design.label.module_parameter'),
                array('field_is_show_index', 'field_headline_index_count', 'field_sort_condition', 'field_item_count', 'field_introduction_count'
                    , 'field_introduction_size','field_image_count', 'field_image_width', 'field_image_height'));
        
    }

    public function render() {
        $obj = new Kd_Object($this->getParameters());
        $pois = App::getModel('customer/collection_shop')
                    ->resetSelect()
                    ->fields('d.shop_name', 'd.id', 'd.photo', 'd.road_id', 'a.sef_url')
                    ->field('d.id', 'poi_id')
                    ->limit($obj->getItemCount());
        if($obj->getSortCondition() == 'clickcount'){
            $obj->joinVisit()->desc('clickcount');
        }
        $pois->load();
        $nameStartIndex = 0;
        $maxRecords = count($pois);
        if($obj->getImageCount()){
            $obj->setIsShowImage(TRUE);
            $obj->setStartImageIndex(0);
            $obj->setEndImageIndex(min($maxRecords, $obj->getImageCount()));
            $nameStartIndex = max($obj->getEndImageIndex(), $nameStartIndex);
        }
        if($obj->getIntroductionCount()){
            $obj->setIsShowIntroduction(TRUE);
            $obj->setStartIntroductionIndex(0);
            $obj->setEndIntroductionIndex(min($maxRecords, $obj->getIntroductionCount()));
            $nameStartIndex = max($obj->getEndIntroductionIndex(), $nameStartIndex);
        }
        $obj->setStartNameIndex($nameStartIndex);
        $obj->setEndNameIndex(min($maxRecords, $obj->getItemCount()));
        $tmpl = $this->getTemplate()->assignMulti(array('pois' => $pois, 'parameters'=>$obj));
        return $tmpl->render($this->getTemplatePath($obj->getTemplateName(), $obj->getTemplateCode()));
    }

}

